package com.lonely.inventory.service.impl;

import com.lonely.common.utils.SecurityUtils;
import com.lonely.inventory.domain.entity.SysStockUpInventory;
import com.lonely.inventory.domain.entity.SysStockUpInventorySizeInfo;
import com.lonely.inventory.service.IStockUpInventoryLogService;
import com.lonely.system.domain.entity.SysBusinessLog;
import com.lonely.system.engine.log.GetLogTimeModuleEnum;
import com.lonely.system.service.IBusinessLogService;
import com.lonely.system.service.ISysFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 黄志标
 * @date: 2025/9/19 10:03
 * @description: 备货库存日志服务实现类
 */
@Service
public class StockUpInventoryLogServiceImpl implements IStockUpInventoryLogService {

    @Autowired
    private IBusinessLogService businessLogService;

    @Autowired
    private ISysFactoryService factoryService;

    /**
     * 新增尾货库存接口的日志
     *
     * @param stockUpInventory
     * @param stockUpInventorySizeInfos
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStockUpInventoryLog(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> stockUpInventorySizeInfos) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(stockUpInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.STOCK_UP_INVENTORY.name());
        sysBusinessLog.setLogTitle("【新增】");
        sysBusinessLog.setLogBody(buildAddStockUpInventoryLogBody(stockUpInventory, stockUpInventorySizeInfos));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 入库备货库存接口的日志
     *
     * @param stockUpInventory
     * @param oldSurplusInventorySizeInfos
     * @param newSurplusInventorySizeInfos
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void putStockUpInventoryLog(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> oldSurplusInventorySizeInfos, List<SysStockUpInventorySizeInfo> newSurplusInventorySizeInfos) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(stockUpInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.STOCK_UP_INVENTORY.name());
        sysBusinessLog.setLogTitle("【入库】");
        sysBusinessLog.setLogBody(buildEditStockUpInventoryLogBody(stockUpInventory, oldSurplusInventorySizeInfos, newSurplusInventorySizeInfos));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 出库备货库存接口的日志
     *
     * @param stockUpInventory
     * @param oldSurplusInventorySizeInfos
     * @param newSurplusInventorySizeInfos
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outStockUpInventoryLog(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> oldSurplusInventorySizeInfos, List<SysStockUpInventorySizeInfo> newSurplusInventorySizeInfos) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(stockUpInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.STOCK_UP_INVENTORY.name());
        sysBusinessLog.setLogTitle("【出库】");
        sysBusinessLog.setLogBody(buildEditStockUpInventoryLogBody(stockUpInventory, oldSurplusInventorySizeInfos, newSurplusInventorySizeInfos));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 调拨备货库存接口的日志
     *
     * @param stockUpInventory
     * @param oldInventorySizeInfos
     * @param newInventorySizeInfos
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transferStockUpInventoryLog(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> oldInventorySizeInfos, List<SysStockUpInventorySizeInfo> newInventorySizeInfos) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(stockUpInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.STOCK_UP_INVENTORY.name());
        sysBusinessLog.setLogTitle("【调拨】");
        sysBusinessLog.setLogBody(buildTransferStockUpInventoryLogBody(stockUpInventory, oldInventorySizeInfos, newInventorySizeInfos));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 备货库存上架接口的日志
     *
     * @param stockUpInventory
     * @param oldInventorySizeInfos
     * @param newInventorySizeInfos
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void putAwayStockUpInventoryLog(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> oldInventorySizeInfos, List<SysStockUpInventorySizeInfo> newInventorySizeInfos) {
        SysBusinessLog sysBusinessLog = new SysBusinessLog();
        sysBusinessLog.setRefId(stockUpInventory.getId());
        sysBusinessLog.setModuleName(GetLogTimeModuleEnum.STOCK_UP_INVENTORY.name());
        sysBusinessLog.setLogTitle("【上架】");
        sysBusinessLog.setLogBody(buildTransferStockUpInventoryLogBody(stockUpInventory, oldInventorySizeInfos, newInventorySizeInfos));
        sysBusinessLog.setCreateBy(SecurityUtils.getUserId());
        sysBusinessLog.setUpdateBy(SecurityUtils.getUserId());
        this.businessLogService.save(sysBusinessLog);
    }

    /**
     * 构建调拨备货库存日志体内容
     *
     * @param stockUpInventory
     * @param oldSurplusInventorySizeInfos
     * @param newSurplusInventorySizeInfos
     * @return
     */
    private String buildTransferStockUpInventoryLogBody(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> oldSurplusInventorySizeInfos, List<SysStockUpInventorySizeInfo> newSurplusInventorySizeInfos) {
        List<String> bodyLines = new ArrayList<>();
        bodyLines.add(String.format("SKC：%s", stockUpInventory.getSkc()));

        List<String> beforeSizeInfo = new ArrayList<>();
        List<String> afterSizeInfo = new ArrayList<>();

        for (int i = 0; i < oldSurplusInventorySizeInfos.size(); i++) {
            SysStockUpInventorySizeInfo currentOldSizeInfo = oldSurplusInventorySizeInfos.get(i);
            beforeSizeInfo.add(String.format("%s-%s:%s", currentOldSizeInfo.getColor(), currentOldSizeInfo.getSizeName(), currentOldSizeInfo.getStockQuantity()));
        }

        for (int i = 0; i < newSurplusInventorySizeInfos.size(); i++) {
            SysStockUpInventorySizeInfo currentNewSizeInfo = newSurplusInventorySizeInfos.get(i);
            afterSizeInfo.add(String.format("%s-%s:%s", currentNewSizeInfo.getColor(), currentNewSizeInfo.getSizeName(), currentNewSizeInfo.getStockQuantity()));
        }
        bodyLines.add(String.format("变动前：%s", String.join("、", beforeSizeInfo)));
        bodyLines.add(String.format("变动后：%s", String.join("、", afterSizeInfo)));

        bodyLines.add(buildOperatorUserInfo());
        return String.join("\r\n", bodyLines);
    }

    /**
     * 构建编辑备货库存日志体内容
     *
     * @param stockUpInventory
     * @param oldSurplusInventorySizeInfos
     * @param newSurplusInventorySizeInfos
     * @return
     */
    private String buildEditStockUpInventoryLogBody(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> oldSurplusInventorySizeInfos, List<SysStockUpInventorySizeInfo> newSurplusInventorySizeInfos) {
        List<String> bodyLines = new ArrayList<>();
        bodyLines.add(String.format("SKC：%s", stockUpInventory.getSkc()));

        List<String> beforeSizeInfo = new ArrayList<>();
        List<String> afterSizeInfo = new ArrayList<>();

        for (int i = 0; i < oldSurplusInventorySizeInfos.size(); i++) {
            SysStockUpInventorySizeInfo currentOldSizeInfo = oldSurplusInventorySizeInfos.get(i);
            SysStockUpInventorySizeInfo currentNewSizeInfo = newSurplusInventorySizeInfos.get(i);

            beforeSizeInfo.add(String.format("%s-%s:%s", currentOldSizeInfo.getColor(), currentOldSizeInfo.getSizeName(), currentOldSizeInfo.getStockQuantity()));
            afterSizeInfo.add(String.format("%s-%s:%s", currentNewSizeInfo.getColor(), currentNewSizeInfo.getSizeName(), currentNewSizeInfo.getStockQuantity()));
        }
        bodyLines.add(String.format("变动前：%s", String.join("、", beforeSizeInfo)));
        bodyLines.add(String.format("变动后：%s", String.join("、", afterSizeInfo)));

        bodyLines.add(buildOperatorUserInfo());
        return String.join("\r\n", bodyLines);
    }

    /**
     * 构建新增备货库存日志体内容
     *
     * @param stockUpInventory
     * @param stockUpInventorySizeInfos
     * @return
     */
    private String buildAddStockUpInventoryLogBody(SysStockUpInventory stockUpInventory, List<SysStockUpInventorySizeInfo> stockUpInventorySizeInfos) {
        List<String> bodyLines = new ArrayList<>();
        bodyLines.add(String.format("SKC：%s", stockUpInventory.getSkc()));

        List<String> beforeSizeInfo = new ArrayList<>();
        List<String> afterSizeInfo = new ArrayList<>();
        for (SysStockUpInventorySizeInfo stockUpInventorySizeInfo : stockUpInventorySizeInfos) {
            beforeSizeInfo.add(String.format("%s-%s:%s", stockUpInventorySizeInfo.getColor(), stockUpInventorySizeInfo.getSizeName(), 0));
            afterSizeInfo.add(String.format("%s-%s:%s", stockUpInventorySizeInfo.getColor(), stockUpInventorySizeInfo.getSizeName(), stockUpInventorySizeInfo.getStockQuantity()));
        }
        bodyLines.add(String.format("变动前：%s", String.join("、", beforeSizeInfo)));
        bodyLines.add(String.format("变动后：%s", String.join("、", afterSizeInfo)));

        bodyLines.add(buildOperatorUserInfo());
        return String.join("\r\n", bodyLines);
    }

    /**
     * 构建操作人信息字符串
     *
     * @return
     */
    private String buildOperatorUserInfo() {
        String factoryName = this.factoryService.getFactoryName(SecurityUtils.getFactoryId());
        return String.format("%s-%s", SecurityUtils.getUsername(), factoryName);
    }
}
